package com.gopay.webtest.controller.cbrmb;

import java.io.UnsupportedEncodingException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gopay.webtest.controller.BackgroundMerController;
import com.gopay.webtest.vo.CommonResp;

/**
 * 商户后台通知
 * 
 * @author 
 * 
 */
@Controller
public class CbRmbNotifyResController {
    @Resource(name = "signHandler")
    private Map<String, Object> signHandler = new HashMap<String, Object>();

    private static Logger logger = Logger
            .getLogger(BackgroundMerController.class);

    /**
     * 处理返回的商户通知
     * 
     * @param request
     * @param response
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/result/BackNotifyMerTest")
    public @ResponseBody
    String resResult(HttpServletRequest request, HttpServletResponse response)
            throws UnsupportedEncodingException {
        
        logger.info("跨境人民币退货通知；");

        request.setCharacterEncoding("gbk");
        ServletRequestDataBinder binder = null;

        logger.info("************************* 接收到返回参数 *****************************");

        CommonResp payinfo = new CommonResp();
        binder = new ServletRequestDataBinder(payinfo);
        binder.bind(request);

        String origSignMsg = payinfo.getSignValue();
        for (Enumeration enumeration = request.getParameterNames(); enumeration.hasMoreElements();) {
            String eleName = (String) enumeration.nextElement();
            String eleValue = request.getParameter(eleName);
            if (logger != null) {
                logger.error("map key: [" + eleName + "]，value: [" + eleValue+ "]");
            }
        }
        try {
            logger.info("ready to encrypted message with md5 or sha ...");

            String[] arrSplit = payinfo.getBackgroundMerUrl().split("[?]");
            String id = "";
            for (String string : arrSplit) {
                String[] param = string.split("=");
                if ("id".equals(param[0])) {
                    id = param[1];
                }
            }
            payinfo.setVerficationCode(id);
            String signMapKey = payinfo.getTranCode();


           /* BaseSign handler = (BaseSign) signHandler.get(signMapKey);
            if (handler == null) {
                throw new Exception("没有符合signMapKey为：【" + signMapKey
                        + "】的加签方式，无法进行加签处理");
            }*/
            
          /*  // 进行加签
            handler.sign(signMapKey, "signType", "signValue", payinfo);*/

            logger.info("end encrypted message with md5 or sha ...");
        } catch (Exception e) {
            payinfo.setRespCode("MR08");
            e.printStackTrace();
        }

        
        return "0000";
    }
}
